n = int(input())
max_dl = 0
if len(str(n)) == 4:
    for i in range(2, n + 1):
        if str(i) in str(n):
            k = False
            for dl in range(2, i // 2 + 1):
                if i % dl == 0:
                    k = True
                    break
            if k:
                continue
            else:
                max_dl = max(max_dl, i)
print(max_dl)
